package com.yl.rabbitmq.day06;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.yl.rabbitmq.utils.RabbitMQUtil;

/**
 * @author yangl
 */
public class ReceiveLog01 {

    public static final String EXCHANGE_NAME = "logs";

    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception{
        Channel channel = RabbitMQUtil.getChannel();
        //声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        //声明一个队列，一个临时队列
        /**
         * 生成一个临时队列，队列名称是随机的，消费者断开队列的时候，队列自动删除
         */
        String queueName = channel.queueDeclare().getQueue();
        /**
         * 交换机与队列绑定
         */
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        System.out.println("Receive01等待接收消息，把接收到的消息打印在屏幕上。。。");
        //接收消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("Receive01控制台打印接收到的消息：" + new String(message.getBody(), "UTF-8"));
        };
        //消费者取消息时回调接口
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {});
    }
}
